package com.liu.curl.utils;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.liu.curl.domain.UserVo;
import com.liu.netty.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class CurlUtil {

    public static String execCurl(String[] cmds) throws  Exception {
        ProcessBuilder process = new ProcessBuilder(cmds);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            return builder.toString();

        } catch (IOException e) {
            System.out.print("error");
            e.printStackTrace();
        }
        return null;

    }

    public static void main(String[] args) throws  Exception {


//        String[] cmds = {"curl", "-H", "Content-Type:application/json", "-X","POST","-d","'"+requestJson+",",""+address+""};


    }

    public static StringBuffer getData(Map<String ,Object> map){
        StringBuffer params=new StringBuffer();
        for(Map.Entry<String,Object> entry : map.entrySet()){
            log.info(""+entry.getKey() +map.get(entry.getKey()));
            params.append(entry.getKey()+"="+map.get(entry.getKey())+"&");
        }
      return params;


    }


    public static String testFromCurl()throws Exception {
        UserVo user = new UserVo();
        user.setUserName("zhangsan");
        user.setPassword("fdasfads");
        String requestJson=JacksonUtil.toJSon(user);
        log.info(requestJson);

        String address ="http://localhost:9005/curl/test01";

        Map map = BeanUtils.beanToMap(user);
        String params =getData(map).toString();
        params=params.substring(0,params.length()-1);

        log.info("拼装参数"+params);


        String[] cmds = {"curl", "-d",""+params+",",""+address+""};
        String result =execCurl(cmds);
        System.out.println(result);
        return result;
    }


    public static void testpreupload(){
        String aa="curl  -d 'path=/apps/appName/filename.jpg&size=2626327&isdir=0&autoinit=1&rtype=3&block_list=[\"60bac7b6464d84fed842955e6126826a\"]&content-md5=60bac7b6464d84fed842955e6126826a&slice-md5=3c5c864d432cc2381b687f8d873e1429'";
        String token="";
        Map map = new HashMap();
        map.put("path","/apps/appName/filename.jpg");
        map.put("size","2626327");
        map.put("isdir","0");
        map.put("autoinit",1);
        map.put("rtype",3);
        map.put("block_list","[\"60bac7b6464d84fed842955e6126826a\"]");
        map.put("content-md5","60bac7b6464d84fed842955e6126826a");
        map.put("slice-md5","60bac7b6464d84fed842955e6126826a");

        String params =getData(map).toString();
        params=params.substring(0,params.length()-1);

        String address ="http://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token="+token+"";
        String[] cmds = {"curl", "-d",""+params+",",""+address+""};

    }


    public static void cutupload(){
        String aa="curl -F 'file=@/Downloads/filename.jpg' \"https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?access_token=1.a6b7dbd428f731037f771b9d15063f61.86410.1292922000-2346618-124528&method=upload&type=tmpfile&path=/apps/AppName/filename.jpg&uploadid=N1-NjEuMTM1LjE2OS44NDoxNTQ1OTY1NTQyOjgzODMxMTY0MDkyNDY2NjQ5Nzg&partseq=0\"";
        String token="";
        String path="";
        String uploadid="";
        String address="https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?access_token="+token+"&method=upload&type=tmpfile&path="+path+"&uploadid="+uploadid+"&partseq=0";


        String[] cmds = {"curl", "-F","file=@\"d:/aaa.jpg\"",""+address+""};


    }


    public static  void createFile(){
        String aa="curl \"https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=123\" -d '&block_list=[\"7d57c40c9fdb4e4a32d533bee1a4e409\"]' -H \"User-Agent: pan.baidu.com\"";
         String token="";
        String address="https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token="+token+"";


        Map map = new HashMap();
        map.put("path","/apps/appName/filename.jpg");
        map.put("size","2626327");
        map.put("isdir","0");

        map.put("uploadid","N1-NjEuMTM1LjE2OS44NDoxNTQ1OTY5MjAzOjgzODQwOTkyMzc2Nzc3MTQwNjc=");
        map.put("rtype",3);
        map.put("block_list","[\"60bac7b6464d84fed842955e6126826a\"]");


        String params =getData(map).toString();
        params=params.substring(0,params.length()-1);
        String[] cmds = {"curl", "-d",""+params+",",""+address+""};



    }


}
